iT邦幫忙

0

【PHP】-Data同項疊加數量-菜鳥筆記(3)

php
  • 分享至 

  • xImage
  •  
$arr = array(
---------------------------
    array(
       'name' => '蘋果',
       'count' => 1,
    ),
	array(
       'name' => '蘋果',
       'count' => 2,
    ),
	array(
       'name' => '蘋果',
       'count' => 3,
    ),
---------------------------
	array(
       'name' => '橘子',
       'count' => 1,
    ),
---------------------------
);
 
$result = array();
 
foreach($arr as $val){
    $key = $val['name'];                           //填入要塞選的項目
    if(!isset($result[$key])){
        $result[$key] = $val;
    }else{
        $result[$key]['count'] += $val['count'];   //填入要疊加的項目
    }
}
print_r($result);
輸出:
Array
(
    [蘋果] => Array
        (
            [name] => 蘋果
            [count] => 6
        )

    [橘子] => Array
        (
            [name] => 橘子
            [count] => 1
        )

)
                            $block_name = explode(",",$v['block_name']);
                            $new_block = array();
                            // echo $v['block_name'];
                            if(in_array(1,$block_name)){
                                $new_block[] = "上午場";
                            }

                            if(in_array(2,$block_name)){
                                // echo ",下午場";
                                $new_block[] = "下午場";
                            }
                            echo implode(',',$new_block);

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言